<template>
  <!-- 整体区域 -->
  <el-container>
    <!-- 头部区域 -->
    <el-header
      ><div>
        <img src="../assets/heima.png" alt="">
        <span>黑马电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button></el-header
    >
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <!-- 1.菜单组件 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          >
          <!-- 2.子菜单：容器 -->
          <el-submenu :index="item.id.toString()" v-for="item in menuList" :key="item.id">
            <!-- 子菜单 文字模板 -->
            <template slot="title">
              <i :class="item.iconclass" class="iconfont"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 3.菜单项：可以用来点击 导航的-->
            <el-menu-item :index="sonItem.id.toString()" v-for="sonItem in item.children" :key="sonItem.id">
              <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{sonItem.authName}}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容区 -->
      <el-main>Main</el-main>
    </el-container>
  </el-container>
</template>

<script>

export default {
  name: 'app',
  data () {
    return {
      // 菜单数组, 从服务器获取到的菜单列表
      menuList: []
    }
  },
  methods: {
    // 退出函数
    logout () {
      //  清空 sessionStorage 数据
      window.sessionStorage.clear()
      // 跳转到登录页面
      this.$router.push('/login')
    }
  },
  // 钩子函数
  async created () {
    const { data: res } = await this.$http.get('menus')
    if (res.meta.status !== 200) return this.$message(res.meta.msg)
    this.menuList = res.data
  }
}
</script>
<style scoped lang="less">
.iconfont {
  margin-right: 10px;
}
.el-header {
  background-color: #363c42;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
     +.el-main {
  background-color: #eaedf1;
}
</style>
